/*
 * Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.
 *
 * https://www.mall4j.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.api.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yami.shop.bean.app.dto.QuestionDto;
import com.yami.shop.bean.app.param.QuestionParam;
import com.yami.shop.bean.model.Question;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.security.api.util.SecurityUtils;
import com.yami.shop.service.QuestionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import cn.hutool.core.bean.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * @author lanhai
 */
@RestController
@RequestMapping("/p/question")
@Tag(name = "反馈问题接口")
@AllArgsConstructor
public class QuestionController {

    @Autowired
    private QuestionService userQuestionService;

    /**
     * 选择订单配送反馈问题
     */
    @GetMapping("/list")
    @Operation(summary = "用户反馈问题列表" , description = "获取用户的所有反馈问题信息")
    public ServerResponseEntity<List<QuestionDto>> dvyList() {
        String userId = SecurityUtils.getUser().getUserId();
        List<Question> userQuestions = userQuestionService.list(new LambdaQueryWrapper<Question>().eq(Question::getUserId, userId).orderByDesc(Question::getQuestionId).orderByDesc(Question::getUpdateTime));
        return ServerResponseEntity.success(BeanUtil.copyToList(userQuestions, QuestionDto.class));
    }

    @PostMapping("/addQuestion")
    @Operation(summary = "新增用户反馈问题" , description = "新增用户反馈问题")
    public ServerResponseEntity<String> addQuestion(@Valid @RequestBody QuestionParam questionParam) {
        String userId = SecurityUtils.getUser().getUserId();

        if (questionParam.getQuestionId() != null && questionParam.getQuestionId() != 0) {
            return ServerResponseEntity.showFailMsg("该反馈问题已存在");
        }
        long questionCount = userQuestionService.count(new LambdaQueryWrapper<Question>().eq(Question::getUserId, userId));
        Question userQuestion = BeanUtil.copyProperties(questionParam, Question.class);

        if (questionCount == 0) {
            // userQuestion.setCommonQuestion(1);
        } else {
            // userQuestion.setCommonQuestion(0);
        }
        userQuestion.setUserId(userId);
        userQuestion.setStatus(1);
        userQuestion.setCreateTime(new Date());
        userQuestion.setUpdateTime(new Date());
        userQuestionService.save(userQuestion);
        // if (userQuestion.getCommonQuestion() == 1) {
        //     // 清除默认反馈问题缓存
        //     userQuestionService.removeQuestionByUserId(0L, userId);
        // }
        return ServerResponseEntity.success("添加反馈问题成功");
    }

    /**
     * 修改订单配送反馈问题
     */
    @PutMapping("/updateQuestion")
    @Operation(summary = "修改订单用户反馈问题" , description = "修改用户反馈问题")
    public ServerResponseEntity<String> updateQuestion(@Valid @RequestBody QuestionParam questionParam) {
        String userId = SecurityUtils.getUser().getUserId();

        Question dbQuestion = userQuestionService.getQuestionByUserId(questionParam.getQuestionId(), userId);
        if (dbQuestion == null) {
            return ServerResponseEntity.showFailMsg("该反馈问题已被删除");
        }

        Question userQuestion = BeanUtil.copyProperties(questionParam, Question.class);
        userQuestion.setUserId(userId);
        userQuestion.setUpdateTime(new Date());
        userQuestionService.updateById(userQuestion);
        // 清除当前反馈问题缓存
        userQuestionService.removeQuestionByUserId(questionParam.getQuestionId(), userId);
        // 清除默认反馈问题缓存
        userQuestionService.removeQuestionByUserId(0L, userId);
        return ServerResponseEntity.success("修改反馈问题成功");
    }

    /**
     * 删除订单配送反馈问题
     */
    @DeleteMapping("/deleteQuestion/{questionId}")
    @Operation(summary = "删除订单用户反馈问题" , description = "根据反馈问题id，删除用户反馈问题")
    @Parameter(name = "questionId", description = "反馈问题ID" , required = true)
    public ServerResponseEntity<String> deleteDvy(@PathVariable("questionId") Long questionId) {
        String userId = SecurityUtils.getUser().getUserId();
        Question userQuestion = userQuestionService.getQuestionByUserId(questionId, userId);
        if (userQuestion == null) {
            return ServerResponseEntity.showFailMsg("该反馈问题已被删除");
        }
        // if (userQuestion.getCommonQuestion() == 1) {
        //     return ServerResponseEntity.showFailMsg("默认反馈问题无法删除");
        // }
        userQuestionService.removeById(questionId);
        userQuestionService.removeQuestionByUserId(questionId, userId);
        return ServerResponseEntity.success("删除反馈问题成功");
    }

    /**
     * 设置默认反馈问题
     */
    @PutMapping("/defaultQuestion/{questionId}")
    @Operation(summary = "设置默认反馈问题" , description = "根据反馈问题id，设置默认反馈问题")
    public ServerResponseEntity<String> defaultQuestion(@PathVariable("questionId") Long questionId) {
        String userId = SecurityUtils.getUser().getUserId();

        userQuestionService.updateDefaultQuestion(questionId, userId);

        userQuestionService.removeQuestionByUserId(0L, userId);
        userQuestionService.removeQuestionByUserId(questionId, userId);
        return ServerResponseEntity.success("修改反馈问题成功");
    }

    /**
     * 获取反馈问题信息订单配送反馈问题
     */
    @GetMapping("/questionInfo/{questionId}")
    @Operation(summary = "获取反馈问题信息" , description = "根据反馈问题id，获取反馈问题信息")
    @Parameter(name = "questionId", description = "反馈问题ID" , required = true)
    public ServerResponseEntity<QuestionDto> questionInfo(@PathVariable("questionId") Long questionId) {
        String userId = SecurityUtils.getUser().getUserId();
        Question userQuestion = userQuestionService.getQuestionByUserId(questionId, userId);
        if (userQuestion == null) {
            throw new YamiShopBindException("该反馈问题已被删除");
        }
        return ServerResponseEntity.success(BeanUtil.copyProperties(userQuestion, QuestionDto.class));
    }

}
